layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 买入超时设置管理
     */
    var SysSellSetting = {
        tableId: "sysSellSettingTable"
    };

    /**
     * 初始化表格的列
     */
    SysSellSetting.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: ''},
            {field: 'overTime', sort: true, title: '支付倒计时（小时）',templet: function (d) {
                    return (d.overTime/60/60);
                }},
            {field: 'isFrozen', sort: true, title: '超时是否冻结账号',templet: function (d) {
                    var type =d.isFrozen;
                    if (type == 0){
                        return "<span style='color: DarkSlateGray  '><b>否 </b></span>";
                    } else if (type == 1){
                        return "<span style='color: DarkTurquoise   '><b> 是</b></span>";
                    }
                }},
            {field: 'isSell', sort: true, title: '是否可以挂售',templet: function (d) {
                    var type =d.isSell;
                    if (type == 0){
                        return "<span style='color: DarkSlateGray  '><b>否 </b></span>";
                    } else if (type == 1){
                        return "<span style='color: DarkTurquoise   '><b> 是</b></span>";
                    }
                }},
            {field: 'isBuy', sort: true, title: '是否可以买入',templet: function (d) {
                    var type =d.isBuy;
                    if (type == 0){
                        return "<span style='color: DarkSlateGray  '><b>否 </b></span>";
                    } else if (type == 1){
                        return "<span style='color: DarkTurquoise   '><b> 是</b></span>";
                    }
                }},
            /*{field: 'createTime', sort: true, title: ''},
            {field: 'updateTime', sort: true, title: ''},
            {field: 'standby1', sort: true, title: ''},*/
            {field: 'standby2', sort: true, title: '确认倒计时（小时）',templet: function (d) {
                    return (d.standby2/60/60);
                }},
            /*{field: 'standby3', sort: true, title: ''},
            {field: 'version', sort: true, title: ''},*/
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    SysSellSetting.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(SysSellSetting.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    SysSellSetting.openAddDlg = function () {
        func.open({
            title: '添加买入超时设置',
            content: Feng.ctxPath + '/sysSellSetting/add',
            tableId: SysSellSetting.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    SysSellSetting.openEditDlg = function (data) {
        func.open({
            title: '修改买入超时设置',
            content: Feng.ctxPath + '/sysSellSetting/edit?id=' + data.id,
            tableId: SysSellSetting.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    SysSellSetting.exportExcel = function () {
        var checkRows = table.checkStatus(SysSellSetting.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    SysSellSetting.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/sysSellSetting/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(SysSellSetting.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + SysSellSetting.tableId,
        url: Feng.ctxPath + '/sysSellSetting/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: SysSellSetting.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        SysSellSetting.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        SysSellSetting.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        SysSellSetting.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + SysSellSetting.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            SysSellSetting.openEditDlg(data);
        } else if (layEvent === 'delete') {
            SysSellSetting.onDeleteItem(data);
        }
    });
});
